<template>
  <div style="margin:30px 20px">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="气瓶类型" prop="productGasType">
        <el-select v-model="queryParams.productGasType" placeholder="请选择气瓶类型" clearable size="small">
          <el-option v-for="dict in productGasTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="进入时间" prop="enteredAt">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.enteredAt" type="date"
          value-format="yyyy-MM-dd" placeholder="选择进入时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="离开时间" prop="leftAt">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.leftAt" type="date"
          value-format="yyyy-MM-dd" placeholder="选择离开时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="small"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small"   @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="small"  @click="handleAdd"  v-hasPermi="['labdanger:cylinder:edit']">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="cylinderList" >
      <el-table-column label="二维码" align="center" prop="productNo" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="使用单位"  prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="cylinderStatusFormat" width="60">
      </el-table-column>
      <el-table-column label="气瓶类型" align="center" prop="productGasType" :formatter="productGasTypeFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="包装"  prop="packageWeight" width="60" align="center"/>
      <el-table-column label="规格"  prop="productSpec" width="60" align="center"/>
      <el-table-column label="数量"  prop="quantity" width="60" align="center"/>
      <el-table-column label="负责人" align="center" prop="ownerName" width="80"/>
      <el-table-column label="进入时间" align="center" prop="enteredAt" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enteredAt, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离开时间" align="center" prop="leftAt" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leftAt, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="100" >
        <template slot-scope="scope" v-if="!scope.row.leftAt">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['labdanger:cylinder:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['labdanger:cylinder:edit']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    </el-col>
    </el-row>
    <!-- 添加或修改气瓶对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="11">
          <el-form-item label="二维码" prop="productNo">
            <el-input v-model="form.productNo" placeholder="请输入气瓶二维码" class="input_dialog_w" size="small"/>
          </el-form-item>
          </el-col>
          <el-col :span="11">
          <el-form-item label="气体名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入气体名称" class="input_dialog_w" size="small"/>
          </el-form-item>
           </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="使用单位" prop="deptName">
              <el-autocomplete  v-model="form.deptName" style="width: 500px;" :disabled = "form.id>0"
                :fetch-suggestions="querySearch" :trigger-on-focus="false" placeholder="请输入实验室名称" @select="handleChoose"
               ></el-autocomplete>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
          <el-form-item label="负责人工号" prop="ownerNo">
            <el-input v-model="form.ownerNo" placeholder="请输入负责人工号" class="input_dialog_w" size="small" @blur="searchUser"
              @keyup.enter.native="searchUser" />
          </el-form-item>
          </el-col>
           <el-col :span="11">
            <el-form-item label="负责人姓名" prop="ownerName">
              <el-input v-model="form.ownerName" placeholder="负责人姓名" disabled class="input_dialog_w" size="small"/>
            </el-form-item>
           </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="气瓶类型" prop="productGasType">
              <el-select v-model="form.productGasType" placeholder="请选择气瓶类型" class="input_dialog_w" size="small">
                <el-option v-for="dict in productGasTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                  :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="包装重量" prop="packageWeight">
              <el-input v-model="form.packageWeight"  class="input_dialog_w" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="规格" prop="productSpec">
              <el-input v-model="form.productSpec"  class="input_dialog_w" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="数量" prop="quantity">
              <el-input v-model="form.quantity" type="number"  class="input_dialog_w" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="进入时间" prop="enteredAt">
              <el-date-picker clearable v-model="form.enteredAt" type="date" size="small"
                value-format="yyyy-MM-dd" placeholder="选择进入时间" style="width: 200px;">
              </el-date-picker>
            </el-form-item>
          </el-col>
           <el-col :span="11">
            <el-form-item label="离开时间" prop="leftAt" v-if="form.id>0">
              <el-date-picker clearable v-model="form.leftAt" type="date" size="small"
                value-format="yyyy-MM-dd" placeholder="选择离开时间" style="width: 200px;">
              </el-date-picker>
            </el-form-item>
           </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCylinder,
    getCylinder,
    delCylinder,
    addCylinder,
    updateCylinder,
    exportCylinder,
  } from "@/api/labbase/cylinder";
  import {
    searchEmployee
  } from "@/api/labdata/employee";
  import {
      searchUserLab
    } from "@/api/labdata/orgnization";
  import {
    getAllGasCylindersType
  } from "@/api/labdata/gasCylinderNum";
  export default {
    name: "Cylinder",
    components: {},
    data() {
      return {
        canAdd: false,
        searchUserInfos: [],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 气瓶表格数据
        cylinderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 气瓶类型字典
        productGasTypeOptions: [],
        // 使用状态字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: null,
          productGasType: null,
          enteredAt: null,
          leftAt: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          productName: [
            { required: true, message: "气体名称不能为空", trigger: "blur" }
          ],
          ownerNo: [
            { required: true, message: "负责人工号不能为空", trigger: "blur" }
          ],
          productGasType: [
            { required: true, message: "气瓶类型不能为空", trigger: "blur" }
          ],
          productSpec: [
            { required: true, message: "规格不能为空", trigger: "blur" }
          ],
          quantity: [
            { required: true, message: "数量不能为空", trigger: "blur" }
          ],
          enteredAt: [
            { required: true, message: "进入时间不能为空", trigger: "blur" }
          ],
        },
      };
    },
    created() {
      this.getAllGasType();
      this.getDicts("cylinderStatusOpt").then(response => {
        this.statusOptions = response.data;
      });
       this.getList();
    },
    methods: {
      searchUser() {
        //console.log(this.form.ownerNo);
        if (!this.form.ownerNo) {
          this.form.ownerName = null;
          return;
        } else
          searchEmployee(this.form.ownerNo).then(response => {
            let searchResult = response.data;
            if (searchResult) {
              this.form.ownerName = searchResult.nickName;
            } else {
              this.form.ownerName = null;
            }
            this.$forceUpdate();
          });

      },
      /** 查询气瓶列表 */
      getList() {
        this.loading = true;
        this.queryParams.deptId = this.$store.state.user.dept.id;
        listCylinder(this.queryParams).then((response) => {
          this.cylinderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getAllGasType() {
        getAllGasCylindersType().then(response => {
          let items = response.data;
          items.forEach(item => {
            this.productGasTypeOptions.push({
              dictValue: item.cylindersTypeId,
              dictLabel: item.name,
            });
          });
        });
      },
      // 气瓶类型字典翻译
      productGasTypeFormat(row, column) {
        return this.selectDictLabel(
          this.productGasTypeOptions,
          row.productGasType
        );
      },
      // 使用状态字典翻译
      cylinderStatusFormat(row, column) {
        if(!this.statusOptions){
          return;
        }
        if(!row.status){
          return;
        }
        return this.selectDictLabels(this.statusOptions, row.status);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        //this.form.deptId = this.$store.state.user.dept.id;
        //this.form.deptName = this.$store.state.user.dept.deptName;
        this.$forceUpdate();
        this.open = true;
        this.title = "添加气瓶";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.$forceUpdate();
        const id = row.id || this.ids;
        getCylinder(id).then((response) => {
          this.form = response.data;
          this.searchUser();
          this.open = true;
          this.title = "修改气瓶";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != null) {
              updateCylinder(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCylinder(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      //输入框获取焦点时调用的方法
      querySearch(queryString, cb) {
        searchUserLab(queryString).then(response => {
          let results = response.data.map((item) => {
            return {
              value: item.parentName+' / '+item.orgName,
              name: item.orgId,
            };
          });
          //cb是回调函数，返回筛选出的结果数据到输入框下面的输入列表
          cb(results);
        });
      },
      handleChoose(item) {
        this.form.deptName = item.value;
        this.$forceUpdate();
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除气瓶编号为"' + row.productNo + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(function() {
            return delCylinder(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          });
      },
    },
  };
</script>
<style type="text/css" scoped>
  .input_dialog_w {
    width: 200px;
  }
</style>
